
package cn.lzh.controller;

import cn.lzh.entity.Order;
import cn.lzh.entity2.OrdInfo;
import cn.lzh.entity2.OrderLine;
import cn.lzh.service.OrderService;
import cn.lzh.tool.RespJson;
import cn.lzh.tool.Token;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/fore/order")
public class OrderForeController {
    @Autowired
    private OrderService orderService;

    //查找该登录用户的下某一个订单号
    @RequestMapping("/findInfoByOrdid/{ordid}")
    public Map<String, Object> findAllOrdersByUid(@PathVariable("ordid") int ordid) {
        int uid = Token.getUid();
        System.err.println(uid);
        if (uid < 0) {
            return RespJson.pack(401, "Token过期， 请重新登录.....", null);
        } else {
            OrdInfo obj = orderService.findOrdAndItemsByOrdid(uid, ordid);
            return RespJson.pack(200, "", obj);
        }
    }


    //查找该登录用户的所有订单头信息
    @RequestMapping("/findOrdLines")
    public Map<String, Object> findAllOrdLines(Integer ispay, Date tstart, Date tend) {
        HashMap<String, Object> map = new HashMap();
        if (ispay != null) {
            map.put("ispay", ispay);
        }

        if (tstart != null) {
            map.put("tstart", tstart);
        }

        if (tend != null) {
            map.put("tend", tend);
        }

        int uid = Token.getUid();
        System.err.println(uid);
        if (uid < 0) {
            return RespJson.pack(401, "Token过期， 请重新登录.....", null);
        } else {
            map.put("uid", uid);
            System.out.println("/findOrdLines 参数:" + map.toString());

            try {
                List<OrderLine> lst = orderService.findOrdLines(map);
                return RespJson.pack(200, "", lst);
            } catch (Exception var7) {
                var7.printStackTrace();
                return RespJson.pack(500, "查询数据有误....", null);
            }
        }
    }


     //分页查询
    @RequestMapping("/findOrdLineByPage/{start}/{num}")
    public Map<String, Object> findAllOrdLineByPages(Integer ispay, Date tstart, Date tend, @PathVariable("start") int start, @PathVariable("num") int num) {
        HashMap<String, Object> map = new HashMap();
        if (ispay != null) {
            map.put("ispay", ispay);
        }

        if (tstart != null) {
            map.put("tstart", tstart);
        }

        if (tend != null) {
            map.put("tend", tend);
        }

        int uid = Token.getUid();
        System.err.println(uid);
        if (uid < 0) {
            return RespJson.pack(401, "Token过期， 请重新登录.....", null);
        } else {
            map.put("uid", uid);
            map.put("startpage", start * num);
            map.put("pagenum", num);
            System.out.println("/findOrdLineByPage/{start}/{num} 参数:" + map);
            int count = orderService.getCount(map);
            HashMap<String, Object> result = new HashMap();
            result.put("total", count);
            try {
                List<OrderLine> lst = orderService.findOrdLines(map);
                result.put("orders", lst);
                return RespJson.pack(200, "", result);
            } catch (Exception var11) {
                var11.printStackTrace();
                return RespJson.pack(500, "查询数据有误....", result);
            }
        }
    }

  //根据订单号获取该订单明细及书本信息
    @RequestMapping("/findItemInfoByOrdid/{ordid}")
    public Map<String, Object> findOrdItemInfo(@PathVariable("ordid") int ordid) {
        List<Map<String, Object>> lst = orderService.findItemInfoByOrdid(ordid);
        System.err.println("xxxxxxxxxx");
        return RespJson.pack(200, "", lst);
    }

    //添加地址
    @RequestMapping("/addAddr")
    public Map<String, Object> Addr(@RequestBody Order ord) {

          orderService.updateOrdAddr(ord);

        return RespJson.pack(200, "添加地址成功 ......", "");
    }


     //付款
    @ResponseBody
    @RequestMapping("/addPay")
    public Map<String, Object> save(@RequestBody Order ord) {
        ord.setIspay(1);
        ord.setTpay(new Date());
       orderService.updateOrdPay(ord);
        Order order = orderService.findOrderById(ord.getOrdid());
        return RespJson.pack(200, "付款成功 ......", order);
    }
}


